<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet 
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <517577550@qq.com>
// +----------------------------------------------------------------------
/**
 * 群组模型
 * @author xinqiyang
 * @date   2010-7-7
 *
 */
class GroupModel extends MetaModel
{


	protected $_validate  = array(
	array ('title','require','组名不能为空',1,'',1),
	array('title','','组名已被使用',1,'unique',1),
	array ('tags','require','关键字不能为空',1,'',1),
	array ('desc','require','简介不能为空',1,'',1),
	//array ('status','require','状态不能为空',1,'',1),
	);


	protected $_auto = array(
	array('status','1'),
	array ('createtime','time',1,'function'),
	array ('user_id','userID',1,'function'),
	);
	//dump view model
	
	/**
	 * 获取成员列表
	 * @param unknown_type $group_id 数组类型 arr
	 * @param unknown_type $limit 条数
	 */
	public function getMembers($group_id,$limit=8)
	{
		 return $this->query("select distinct sz_user.id,sz_user.city,sz_user.cnname,sz_user.image_id,sz_image.url,sz_image.filename,sz_image.model  from sz_user,sz_userandgroup,sz_image where sz_user.id =sz_userandgroup.user_id and  sz_image.id=sz_user.image_id and  sz_userandgroup.group_id in $group_id order by sz_userandgroup.createtime desc limit 0,$limit ");
		
	}
	
	public function getTopics($group)
	{
			
		return $this->query("
			SELECT sz_user.id as userid, sz_user.cnname , sz_group.id as groupid, sz_group.title as groupname, sz_topic.id as topicid, sz_topic.title,sz_topic.lastreplytime,sz_topic.postcount,sz_topic.id,sz_topic.status
			FROM sz_user, sz_group, sz_topic
			WHERE sz_topic.user_id = sz_user.id
			AND sz_topic.group_id = sz_group.id
			AND sz_topic.status <> 2
			AND sz_topic.group_id in $group
			ORDER BY sz_topic.status desc,sz_topic.lastreplytime desc
			");
		
		
	}
	
	public function getGroup($arr)
	{
		
		return $this->query("
		SELECT sz_group.id,sz_group.title,sz_group.membercount,sz_group.topiccount,sz_image.url,sz_image.filename,sz_image.model
		FROM sz_image,sz_group
		WHERE sz_group.image_id = sz_image.id 
		AND sz_group.id in ".$arr
		);
	}
	
	
}
?>